package com.example.administrator.jingpingou.fragment;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.activity.NextThemeActivity;
import com.example.administrator.jingpingou.adapter.NextThemeTwoAdapter;
import com.example.administrator.jingpingou.bean.NextThemeBean;
import com.example.administrator.jingpingou.config.NetConfig;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class NextThemeTwo extends AppCompatActivity {

    private ListView mListView;
    private NextThemeTwoAdapter mAdapter;
    private List<NextThemeBean.DataBean> mData;
    private String zid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next_theme_two);
        Intent intent =getIntent();
        zid = intent.getStringExtra("zid");
        initView();
        initData();
        setData();
    }

    private void setData() {
        mListView.setAdapter(mAdapter);
    }

    private void initData() {
        mData =new ArrayList<>();
        mAdapter =  new NextThemeTwoAdapter(mData,this);
        loadSupport();

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mData.get(position);
                Intent intent = new Intent(NextThemeTwo.this,NextThemeActivity.class);
                intent.putExtra("zid",zid);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.lv_activity_next_theme_two);
    }

    public void loadSupport(){
        String pathSupport = String.format(NetConfig.PATH_THEME_NEXT,zid);
        Log.d("tag", "loadSupport: "+pathSupport);
        x.http().get(new RequestParams(pathSupport), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result!=null) {
                    Gson gson = new Gson();
                    NextThemeBean nextThemeBean = gson.fromJson(result, NextThemeBean.class);
//                    currentPage++;
                    mData.addAll(nextThemeBean.getData());
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
